/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package anton.dinner.menu;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

/**
 *
 * @author johan
 */
@SessionScoped
@ManagedBean(name="listDinners")

public class ListDinner implements Serializable {
    
    
     @Resource(name="jdbc/antonsDatasource")
             private DataSource ds;
    
     private ArrayList<DinnerEntity> dinnerList;
     
     private DinnerEntity dinner;
   
     private String sql;
     

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }
     

    public ArrayList<DinnerEntity> getMenu() throws SQLException
    
    { 
         
            dinnerList = new ArrayList<>();
   
		if(ds==null)
			throw new SQLException("Can't get data source");// throws exception i database not available
        try (Connection con = ds.getConnection()) {
            if(con==null)
                throw new SQLException("Can't get database connection");
            PreparedStatement ps
                    = con.prepareStatement(
                            sql); // query select from dinner specified attributes
            
            //get  item info from database
            ResultSet result =  ps.executeQuery();
              
            
            while(result.next()){
                
                dinner=new DinnerEntity();
                dinner.setDinnerName(result.getString("dinner_name"));
                dinner.setDinnerDescription(result.getString("dinner_description"));
                dinner.setDinnerPrice(result.getInt("dinner_price"));
                dinner.setDinnerType(result.getString("dinner_type"));
                
                
                //adds ingredients to list
                dinnerList.add(dinner);
            }
        }
		
    
    return dinnerList;
    }
       
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
